﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Vortex.Utils.Reflection {

	///<summary>Implements value proxy for field</summary>
	public class FieldValueProxy : AbstractValueProxy {
		private FieldInfo _fieldInfo;

		/// <summary>
		/// Initializes a new instance of the <see cref="FieldValueAccessor"/> class.
		/// </summary>
		/// <param name="propertyInfo">The field info.</param>
		/// <param name="instance">The target instance. May be null if property is static.</param>
		public FieldValueProxy(FieldInfo fieldInfo, object instance)
			: base(instance) {
			if (null == fieldInfo) throw new ArgumentNullException("fieldInfo");

			_fieldInfo = fieldInfo;
		}

		/// <summary>
		/// Gets the name of field.
		/// </summary>
		/// <value>The name of field.</value>
		public override string Name {
			get { return _fieldInfo.Name; }
		}

		/// <summary>
		/// Gets the type of the value.
		/// </summary>
		/// <value>The type of the value.</value>
		public override Type ValueType {
			get { return _fieldInfo.FieldType; }
		}

		/// <summary>
		/// Gets or sets the value of target instance.
		/// </summary>
		/// <value>The value of value provider (field, property else).</value>
		public override object Value {
			get { return _fieldInfo.GetValue(Instance); }
			set { _fieldInfo.SetValue(Instance, value); }
		}
	}
}
